package com.lzk.org.service.impl;

import com.lzk.basic.excption.BusinessException;
import com.lzk.basic.service.impl.BaseServiceImpl;
import com.lzk.basic.util.BaiduAuditUtils;

import com.lzk.org.domain.Employee;
import com.lzk.org.domain.Shop;
import com.lzk.org.domain.ShopAuditLog;
import com.lzk.org.dto.ShopDto;
import com.lzk.org.mapper.EmployeeMapper;
import com.lzk.org.mapper.ShopAuditLogMapper;
import com.lzk.org.mapper.ShopMapper;
import com.lzk.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;


@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    /**
     * 商家入驻逻辑：
     * 一：校验
     * 1.1.每个参数的空值判断
     * 1.2.两次密码不一致校验
     * 1.3.该店铺是否已经被入驻过
     * 二：入驻业务
     * 2.1.保存店铺管理员信息t_employee
     * 2.2.保存店铺信息t_shop
     * 2.2.将店铺的id更新到t_employee中
     */
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {
        //一：检验
        //1.1.空值校验 - 前端js校验了后端也要校验，因为前端js校验不安全可以跳过
        //这里就校验了一个，其实其他也需要空值校验
        if (StringUtils.isEmpty(shop.getName()) ||
           StringUtils.isEmpty(shop.getTel()) ||
           StringUtils.isEmpty(shop.getAddress()) ||
           StringUtils.isEmpty(shop.getShopManager().getPhone()) ||
           StringUtils.isEmpty(shop.getShopManager().getEmail()) ||
           StringUtils.isEmpty(shop.getShopManager().getUsername()) ||
           StringUtils.isEmpty(shop.getShopManager().getPassword())||
           StringUtils.isEmpty(shop.getShopManager().getComfirmPassword())
        ) {
            throw new BusinessException("参数不能为空!");
        }

        //校验完成后，进行百度AI审核
        Boolean aBoolean = BaiduAuditUtils.TextCensor(shop.getName());
        if(!aBoolean){//审核不通过
            throw new BusinessException("店铺名称违规,请重新输入!");
        }

        //1.2.两次密码不一致校验
        Employee admin = shop.getShopManager();
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一致，请重新输入!");
        }
        //1.3.该店铺是否已经被入驻过
        Shop shopTemp = shopMapper.loadByNameAndAddress(shop);
        if(shopTemp != null){
            throw new BusinessException("店铺已经入驻，请直接登陆！忘记密码请找回密码");
        }

        //二：入驻业务
        //2.1.保存店铺管理员信息t_employee
        employeeMapper.save(admin);
        //2.2.保存店铺信息t_shop
        shop.setAdmin_id(admin.getId());
        shopMapper.save(shop);
        //2.3.将店铺的id更新到t_employee中
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }

    /**
     * 审核通过业务实现
     * @param log
     * @throws MessagingException
     */
    @Override
    @Transactional
    public void pass(ShopAuditLog log) throws MessagingException {
        //1.修改状态：state = 2
        Shop shop = shopMapper.loadById(log.getShop_id());
        shop.setState(2);
        //更新state字段
        shopMapper.update(shop);

        //2.添加审核日志到数据库
        log.setState(2);
        //暂时无法知道谁登录了，假数据
        log.setAudit_id(321L);
        shopAuditLogMapper.save(log);

        //3.发送邮件：审核通过邮件 - 叫他激活
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("563238600@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208/group1/M00/00/B9/rBE3kWD_fEuALou2AAB4rV4bI7c231.jpg' ><a href='http://localhost:8080/shop/active/22'>点击该链接激活</a>",true);
        //添加附件
        helper.addAttachment("1.jpg",new File("C:\\Users\\ki\\Desktop\\aa\\1.jpg"));
        //收件人
        helper.setTo("563238600@qq.com");
        javaMailSender.send(mimeMessage);
    }

    /**
     * 审核驳回业务实现
     * @param log
     */
    @Override
    @Transactional
    public void reject(ShopAuditLog log) throws MessagingException {
        Shop shop = shopMapper.loadById(log.getShop_id());

        //添加到审核日志到数据库
        log.setState(4);
        log.setAudit_id(325L);
        shopAuditLogMapper.save(log);

        //3.发送邮件：驳回邮件 - 告诉他审核失败，重新入驻
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("563238600@qq.com");
        helper.setSubject("店铺驳回邮件");
        helper.setText("<h1>店铺入驻失败，请重新入驻!!!</h1><img src='http://123.207.27.208/group1/M00/00/B9/rBE3kWD_fEuALou2AAB4rV4bI7c231.jpg' ><a href='http://localhost:8080/shop/active/22'>点击该链接激活</a>",true);
        //添加附件
        helper.addAttachment("1.jpg",new File("C:\\Users\\ki\\Desktop\\aa\\1.jpg"));
        //收件人
        helper.setTo("563238600@qq.com");
        javaMailSender.send(mimeMessage);
        //4.删除入驻信息
        //删除管理员信息
        employeeMapper.remove(shop.getAdmin_id());
        //删除店铺信息
        shopMapper.remove(shop.getId());
    }

    @Override
    public List<ShopDto> getCountByState() {
        return shopMapper.getCountByState();
    }
}
